iT邦幫忙

2023 iThome 鐵人賽

0
Modern Web

前端知識系列 第 29

10 React Antipatterns to Avoid - Code This, Not That!

  • 分享至 

  • xImage
  •  

Yes

React Anti-patterns and Solutions

Anti-pattern 1: Big Components

  • Problem: Starting off with one big, deeply nested component.
  • Solution: Refactor into smaller, reusable components.

Anti-pattern 2: Nesting Components

  • Problem: Defining child components inside parent components.
  • Solution: Define child components outside the parent component.

Anti-pattern 3: Failure to Memoize

  • Problem: Running expensive calculations every time the state changes.
  • Solution: Use the `useMemo` hook.

Anti-pattern 4: Useless Divs

  • Problem: Wrapping multiple elements in unnecessary `div` tags.
  • Solution: Use React Fragments.

Anti-pattern 5: Messy Files

  • Problem: Exporting multiple components from a single file.
  • Solution: One component per file.

Anti-pattern 6: Big Bundles

  • Problem: Slow initial page load due to large JavaScript bundles.
  • Solution: Use code splitting and lazy loading.

Anti-pattern 7: Prop Drilling

  • Problem: Passing state through multiple layers of components.
  • Solution: Use context API for global state management.

Anti-pattern 8: Prop Plowing

  • Problem: Components with a lot of different props.
  • Solution: Use the spread syntax.
    https://ithelp.ithome.com.tw/upload/images/20231029/20162110sqe99VeFip.png

Anti-pattern 9: Messy Event Handlers

  • Problem: Using arrow functions for event handlers with multiple arguments.
  • Solution: Use curried functions.
    https://ithelp.ithome.com.tw/upload/images/20231029/20162110aPUwp4jfn2.png

Anti-pattern 10: Single Object State

  • Problem: Using a single object for all state in a component.
  • Solution: Use multiple `useState` hooks, and extract to custom hooks

上一篇
Better optimistic update
下一篇
ts-reset
系列文
前端知識30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言